package yanyan.com.tochar;

import android.app.Activity;
import android.app.ProgressDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.HttpUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * 短链接生成
 */
public class FunsShotLinkActivity extends Activity {

    private EditText linkUrlEd, resEd;
    private ProgressDialog loadin;
    private Spinner typeSp;
    private ArrayAdapter<String> typeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_link);
        linkUrlEd = findViewById(R.id.link_url);
        resEd = findViewById(R.id.link_res);
        typeSp = findViewById(R.id.link_type);

        List<String> typeList = new ArrayList();
        typeList.add("u6.gg");
        typeList.add("sou.im");
        typeAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, typeList);
        typeAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        typeSp.setAdapter(typeAdapter);

        //统计各功能
        CoreUtil.statisticalFuns(this, "funs_shot_link");
    }


    /**
     * key:Ix52DlGuDhyY0YdrBE@ddd
     *
     * @param view
     */
    public void getShortLink(View view) {
        try {
            String link = linkUrlEd.getText().toString();
            if (link.length() < 6) {
                ToastUtil.showLongToast(this, "请输入正确的链接");
                return;
            }
            loadin = new ProgressDialog(this);
            loadin.setMessage("生成中...");
            loadin.show();
            int selectedItemPosition = typeSp.getSelectedItemPosition();
            String reqUrl = "";
            if (selectedItemPosition == 0) {
                reqUrl = "http://api.ft12.com/api.php?url=" + link + "&apikey=Ix52DlGuDhyY0YdrBE@ddd";
            } else {
                reqUrl = "http://suo.im/api.htm?url="+link+
                        "&key=5e9c024f3a005a12b5e62db5@ee70301e7e51729b1d5af6dba085d2a6&expireDate=2030-03-31";
            }

            Call msg = HttpUtil.getMsg(reqUrl);
            msg.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    loadin.cancel();
                    ToastUtil.showLongToast(FunsShotLinkActivity.this, "生成错误");
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String res = response.body().string();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            loadin.cancel();
                            resEd.setText(res);
                        }
                    });
                }
            });

        } catch (Exception e) {

        }
    }

}
